package com.jht.bletool2kotlin.activity.splash


import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.WindowInsets
import android.view.WindowManager

import com.jht.bletool2kotlin.R
import com.jht.bletool2kotlin.activity.BaseActivity
import com.jht.bletool2kotlin.activity.homepage.HomeActivity
import com.jht.bletool2kotlin.util.DateUtil
import kotlinx.android.synthetic.main.activity_splash.*


private const val TAG = "SplashActivity"

class SplashActivity : BaseActivity(),ISplash.UI{
    private val shortAnimationDuration: Int = 1500
    private lateinit var presenter : ISplash.Presenter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG, "onCreate: ")
    }

    override fun getLayoutID(): Int {
        return R.layout.activity_splash
    }

    override fun initView() {
        presenter = SplashPresenter(this)
        presenter.startSplash()
    }

    @Suppress("DEPRECATION")
    override fun setStatus() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
            Log.d(TAG, "setStatus: ")
            window.insetsController?.hide(WindowInsets.Type.statusBars())
        }else{
            window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        }
    }

    override fun showVersionUpdateInformation() {
        showToast("showVersionUpdateInformation")
    }

    override fun startHomeActivity() {
        iv_splash_show.animate()
            .alpha(1f)
            .setDuration(shortAnimationDuration.toLong())
            .setListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    goHomeActivity()
                    Log.d(TAG, "onAnimationEnd: "+ DateUtil.getCurrentTime())
                }
            })
    }

    fun goHomeActivity(){
        val intent = Intent(this, HomeActivity::class.java)
        startActivity(intent)
        finish()
    }

}